package com.i_art_engineer.meigong.activity;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.i_art_engineer.meigong.AppContext;
import com.i_art_engineer.meigong.R;
import com.i_art_engineer.meigong.base.FullScreenActivity;

import java.util.Set;

/**
 * 常用语界面
 */
public class PhrasesActivity extends FullScreenActivity {

    private ListView cantlist;
    private ImageView iv_back;
    private TextView tv_newPhrase;

    private LoaderManager manager;

    // 常用语列表
    private Set<String> phrases;
    CursorAdapter adapter;

    private static final String KEY_PHRASES_DATA = "data.user.phrases";

    AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ImageView iv_edit = (ImageView) view.findViewById(R.id.iv_phrase_edit);
            iv_edit.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Toast.makeText(PhrasesActivity.this, "编辑按钮点击了", Toast.LENGTH_SHORT).show();
                }
            });
            ImageView iv_delete = (ImageView) view.findViewById(R.id.iv_phrase_delete);
            final int loc = position;
            iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(PhrasesActivity.this, "删除按钮点击了", Toast.LENGTH_SHORT).show();
                }
            });
        }
    };

    View.OnClickListener listenerActionBar = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_back:
                    finish();
                    break;
                case R.id.newPhrase:

                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phrases);

        //ActionBar
        iv_back = (ImageView) findViewById(R.id.btn_back);
        tv_newPhrase = (TextView) findViewById(R.id.newPhrase);
        iv_back.setOnClickListener(listenerActionBar);
        tv_newPhrase.setOnClickListener(listenerActionBar);

        initPhrases();

    }

    /**
     * 初始化常用语加载
     */
    public void initPhrases() {
        cantlist = (ListView) findViewById(R.id.list_cantlist);

        Cursor phrases = AppContext.db().getPhrasesByUser("hashids");

//        String[] from = {TABLE_NAME_NAME,TABLE_NAME_PHONE};//列名与控件id一一对应
//        int[] to = {R.id.item_tv_name,R.id.item_tv_phone};

        ListAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1,
                phrases,
                new String[]{"FirstName"},
                new int[]{android.R.id.text1}, 0);

        cantlist.setAdapter(adapter);
    }

//    public void createCursorAdapter(Cursor cursor) {
//        //游标适配器,构造方法，传入cursor
//        adapter = new CursorAdapter(this, cursor)
//        {//重写两个方法
//            @Override
//            public View newView (Context context, Cursor cursor, ViewGroup parent)
//            {
//                //找到布局和控件
//                ViewHolder holder = new ViewHolder();
//                LayoutInflater inflater = getLayoutInflater();
//                View inflate = inflater.inflate(R.layout.listview_item, null);
//                holder.item_tv_name = (TextView) inflate.findViewById(R.id.item_tv_name);
//                holder.item_tv_phone = (TextView) inflate.findViewById(R.id.item_tv_phone);
//                inflate.setTag(holder);
//                return inflate;//返回的view传给bindView。
//            }
//
//            @Override
//            public void bindView (View view, Context context, Cursor cursor)
//            {
//                //复用布局
////                把数据设置到界面上
//                ViewHolder holder = (ViewHolder) view.getTag();
//                String name = cursor.getString(cursor.getColumnIndex("name"));
//                String phone = cursor.getString(cursor.getColumnIndex("phone"));
//
//                holder.item_tv_name.setText(name);
//
//                holder.item_tv_phone.setText(phone);
//            }
//        }
//    }

    ;

}
